/**
 * WangSong 2009-10-16 增加两个put方法,化简传入参数
 */
package cn.biplam.front.template.control;

import java.util.ArrayList;

import cn.biplam.common.tools.TOOLS;
import cn.biplam.common.wrapper.ErrorMessage;
import cn.biplam.front.html.HtmlAnchor;
import cn.biplam.front.html.HtmlDiv;
import cn.biplam.front.html.HtmlLI;
import cn.biplam.front.html.HtmlUL;
import cn.biplam.front.html.base.HtmlWindow;
import cn.biplam.front.html.base.Const.Window_Event;
import cn.biplam.front.htmlscript.script.AjxScript;

/**
 * 界面切换控件,用于需要在一个窗口中实现多个界面之间的切换 
 * 
 * <ul>用法：
 * 	<li> new Panel("页面ID")  创建页面却换控件
 * 	<li> this.putLabel("标题","切换的动作ID","是否默认选项页");  放入切换页
 * 	<li> this.setSize 设置控件占位大小
 * 	<li> this.get()	 获取控件
 * </ul>
 * @author XiaoRuxing 2009-7-13下午07:11:32
 * @version 1.0
 */
public class Panel {

	/** 控件页面ID */
	private String pageID;
	private HtmlDiv page;

	/** 切换标签ID */
	private String labelID;
	/** 切换标签 */
	private HtmlDiv label;
	private HtmlUL labelUL;

	/** 内容显示区ID */
	private String domainID;
	/** 内容显示区域 */
	private HtmlDiv domain;

	/** 默认选择项ID */
	private String defautItemID=null;

	private ArrayList<String[]> labels;

	/**
	 * 传入页面ID创建切换控件
	 * @param pageID 页面ID
	 */
	public Panel(String pageID){
		this.pageID = pageID;
		this.labelID = pageID+"label";
		this.domainID = pageID+"domain";
		this.page = new HtmlDiv(this.pageID,"pnl");
		this.label = new HtmlDiv(this.labelID,"tabl");
		this.domain = new HtmlDiv(this.domainID,"pnld");
		this.labelUL = new HtmlUL(null,"tab");
		this.labels=new ArrayList<String[]>();
	}

	/**
	 * 放入切换标签
	 * @param labelTitle  切换标签标题 
	 * @param actionID    装载切换标签动作ID
	 * @throws ErrorMessage 
	 */
	public void put(String labelTitle,String actionID) throws ErrorMessage{
		this.put(labelTitle,actionID," ");
	}

	/**
	 * 放入切换标签
	 * @param labelTitle  切换标签标题 
	 * @param actionID    装载切换标签动作ID
	 * @param recordID    记录ID
	 * @throws ErrorMessage 
	 */
	public void put(String labelTitle,String actionID,String recordID) throws ErrorMessage{
		this.put(labelTitle,actionID,recordID,false);
	}

	/**
	 * 放入切换标签
	 * @param labelTitle  切换标签标题 
	 * @param actionID    装载切换标签动作ID
	 * @param recordID    记录ID
	 * @param isDefault   是否默认选择项
	 * @throws ErrorMessage 
	 */
	public void put(String labelTitle,String actionID,String recordID,boolean isDefault) throws ErrorMessage{
		String[] label=new String[]{labelTitle,
									actionID,
									recordID,
									TOOLS.booleanToString(isDefault)
								};
		this.labels.add(label);
	}

	/**
	 * 获取页面切换界面
	 * @return
	 * @throws ErrorMessage
	 */
	public HtmlWindow get() throws ErrorMessage{
		this._putLabel();
		this.label.put(labelUL);
		this.page.put(label);
		this.page.put(domain);
		this.page.bind("panelResize('"+this.pageID+"');");
		if(this.defautItemID!=null){
			this.page.bind("panelDefault('"+this.defautItemID+"');");
		}else{
			this.page.bind("panelDefault('"+this.labelID+"','1');");
		}
		return this.page;
	}



	/**
	 * 放入所有标签 
	 * @throws ErrorMessage
	 */
	private void _putLabel() throws ErrorMessage{
		/** String[0:title,1:action,2:recordID,3:isDefault(0/1)] */
		for(int i=0;i<this.labels.size();i++){
			String[] label=this.labels.get(i);
			String labelTitle=label[0];
			String actionID=label[1];
			String recordID=label[2];
			boolean isDefault=TOOLS.stringToBoolean(label[3]);
			String labelID=String.valueOf(i);

			String script = AjxScript.getM(this.pageID, actionID, recordID,this.domainID);
			HtmlAnchor anchor = new HtmlAnchor(null,"taba");
			anchor.put(labelTitle);
			anchor.setHref("#");
			HtmlLI li = new HtmlLI(this.labelID+labelID,"tabitem");
			li.put(anchor);
			li.bind(Window_Event.OnClick, script+"panelChange(this);");
			this.labelUL.put(li);
			if(isDefault){
				this.defautItemID = this.labelID+labelID;
			}
		}
	}
}
